package com.example.drainage.ui.downwin

import android.text.TextUtils
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.toColorInt
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.arm.base.BaseApplication
import com.example.arm.ext.dpInt
import com.example.drainage.R
import com.example.drainage.base.ktexpand.setSelectTextColor

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.downwin
 * @createTime 创建时间 ：2021/12/9
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class DownWinTextAdapter(private val isShowRightDrawable: Boolean = false) :
    BaseQuickAdapter<DownWinTextAdapter.DownWinTextBean, BaseViewHolder>(0) {

    var selectPosition = -1

    var onSelectListener: ((Int, DownWinTextBean) -> Unit)? = null

    private val rightDrawable = ResourcesCompat.getDrawable(BaseApplication.INSTANCE.resources, R.mipmap.icon_cert_work_order_select, null)!!

    init {
        rightDrawable.setBounds(0, 0, rightDrawable.minimumWidth, rightDrawable.minimumHeight)
    }

    override fun convert(holder: BaseViewHolder, item: DownWinTextBean) {
        (holder.itemView as TextView).apply {
            text = item.text
            isSelected = selectPosition == holder.absoluteAdapterPosition
            if (isShowRightDrawable && selectPosition == holder.absoluteAdapterPosition) {
                setCompoundDrawables(null, null, rightDrawable, null)
            } else {
                setCompoundDrawables(null, null, null, null)
            }
            setOnClickListener {
                if (selectPosition == holder.absoluteAdapterPosition) {
                    return@setOnClickListener
                }
                selectPosition = holder.absoluteAdapterPosition
                onSelectListener?.invoke(holder.absoluteAdapterPosition, item)
                notifyDataSetChanged()
            }
        }
    }

    override fun createBaseViewHolder(parent: ViewGroup, layoutResId: Int): BaseViewHolder {
        val textView = TextView(parent.context).apply {
            setSelectTextColor("#FF1A7AFE".toColorInt(), "#FF373C43".toColorInt())
            layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 44.dpInt)
            gravity = Gravity.CENTER_VERTICAL
            setPadding(10.dpInt, 0, 0, 0)
            maxLines = 2
            ellipsize = TextUtils.TruncateAt.END
        }
        return createBaseViewHolder(textView)
    }

    class DownWinTextBean(val text: String, val content: Any? = null)
}